package project.parser.ast.aux;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import static java.util.Collections.unmodifiableList;
import static project.parser.tokenizer.TokenType.COMMA;

public abstract class AuxList<T> implements Iterable<T> { //classe astratta per gestire una lista immutabile di oggetti
	protected final List<T> list;

	public AuxList(List<T> list) {
		if (list == null)
			list = new LinkedList<>();
		this.list = unmodifiableList(list);
	}

	public T get(int index) {
		return list.get(index);
	}

	public int length() {
		return list.size();
	}

	public boolean isVoid() {
		return list.size() == 0;
	}

	@Override
	public Iterator<T> iterator() {
		return list.iterator();
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		int i = 0, size = length();
		for (T elem : this) {
			sb.append(elem.toString());
			if (++i != size)
				sb.append(COMMA + " ");
		}
		return sb.toString();
	}
}
